#include "FontRenderer.hpp"

FontRenderer::FontRenderer(int size, std::string fontToLoad) : font(NULL) //: font("./resources/fonts/default/default.ttf")
{
  if (size < 1)
    size = 1;

  std::string tmp = fontToLoad;
  const char *fnt = tmp.c_str();
  LOG_INFO << "Loading font: " << fnt << "\n";
  font = new FTBufferFont(fnt);

  if (!Error())
  {
    font->FaceSize(size);
  }
  else
  {
    LOG_ERROR << "Font [" << fontToLoad << "] could not be initialized!\n";
  }
}

FontRenderer::~FontRenderer()
{
  LOG_INFO << "~FontRenderer()";
  delete font;
}

void FontRenderer::Render(std::string s, Vec2i pos, Vec3d color)
{
  return;
  if (!Error())
  {
    glColor3d(color.x, color.y, color.z);
    font->Render(s.c_str(),-1, FTPoint(pos.x, pos.y,0), FTPoint(), FTGL::RENDER_BACK);
    glBindTexture(GL_TEXTURE_2D, 0);
  }
}

Vec2i FontRenderer::GetSizeOf(std::string s)
{
  if (!Error())
  {
    FTBBox size = font->BBox(s.c_str());
    return Vec2i(    (int) ( size.Upper().X() - size.Lower().X() ),
                    (int) ( size.Upper().Y() - size.Lower().Y() ) );
  }
  return Vec2i(0,0);
}

bool FontRenderer::Error()
{
  if (font == NULL)
    return true;
  return (font->Error() != 0);
}
